//
//  CrashLogger.swift
//  swift_review
//
//  Created by Andrew on 2024/12/23.
//

import UIKit
//
//class CrashLogger {
//    static func setupCrashHandlers() {
//        // 捕获 Objective-C 异常
//        NSSetUncaughtExceptionHandler(handleUncaughtException)
//        
//        // 捕获 Swift 信号
//        let signals: [Int32] = [SIGABRT, SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGPIPE]
//        for signalCode in signals {
//            signal(signalCode) { signal in
//                handleSignal(signal)
//            }
//        }
//    }
//
//    /// 恢复默认信号处理程序
//    static func unregisterSignalHandlers() {
//        let signals: [Int32] = [SIGABRT, SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGPIPE]
//        for signalCode in signals {
//            signal(signalCode, SIG_DFL)
//        }
//    }
//
//    /// 保存崩溃日志到文件
//    static func saveCrashLog(_ log: String) {
//        guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
//            return
//        }
//        let logFilePath = documentDirectory.appendingPathComponent("crash_log.txt")
//
//        if FileManager.default.fileExists(atPath: logFilePath.path) {
//            if let fileHandle = try? FileHandle(forWritingTo: logFilePath) {
//                fileHandle.seekToEndOfFile()
//                if let data = ("\n\n\(log)").data(using: .utf8) {
//                    fileHandle.write(data)
//                }
//                fileHandle.closeFile()
//            }
//        } else {
//            try? log.write(to: logFilePath, atomically: true, encoding: .utf8)
//        }
//        print("Crash log saved at: \(logFilePath.path)")
//    }
//}
//
///// 全局函数：未捕获异常处理程序
//func handleUncaughtException(exception: NSException) {
//    let log = """
//    ======= UNCAUGHT EXCEPTION =======
//    Name: \(exception.name)
//    Reason: \(exception.reason ?? "Unknown reason")
//    User Info: \(exception.userInfo ?? [:])
//    Stack Trace:
//    \(exception.callStackSymbols.joined(separator: "\n"))
//    ==================================
//    """
//    CrashLogger.saveCrashLog(log)
//    print(log)
//}
//
///// 全局函数：信号处理程序
//func handleSignal(_ signal: Int32) {
//    let log = """
//    ======= SIGNAL CAUGHT =======
//    Signal: \(signal)
//    Stack Trace:
//    \(Thread.callStackSymbols.joined(separator: "\n"))
//    ============================
//    """
//    CrashLogger.saveCrashLog(log)
//    print(log)
//    CrashLogger.unregisterSignalHandlers()
//    exit(signal)
//}

//
//// 信号处理函数
//func signalHandler(signal: Int32) {
//    let log = """
//    ======= SIGNAL CAUGHT =======
//    Signal: \(signal)
//    Stack Trace:
//    \(Thread.callStackSymbols.joined(separator: "\n"))
//    ============================
//    """
//    
//    // 保存崩溃日志
//    CrashLogger.saveCrashLog(log)
//    print(log)  // 打印日志
//    // 退出应用
//    exit(signal)
//}
//
//// 设置信号处理
//func setupSignalHandler() {
//    signal(SIGABRT, signalHandler)  // 捕获 SIGABRT 信号
//}
//
//class CrashLogger {
//    static func saveCrashLog(_ log: String) {
//        guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
//            return
//        }
//        let logFilePath = documentDirectory.appendingPathComponent("crash_log.txt")
//
//        if FileManager.default.fileExists(atPath: logFilePath.path) {
//            if let fileHandle = try? FileHandle(forWritingTo: logFilePath) {
//                fileHandle.seekToEndOfFile()
//                if let data = ("\n\n\(log)").data(using: .utf8) {
//                    fileHandle.write(data)
//                }
//                fileHandle.closeFile()
//            }
//        } else {
//            try? log.write(to: logFilePath, atomically: true, encoding: .utf8)
//        }
//        print("Crash log saved at: \(logFilePath.path)")
//    }
//}
